"use strict";
{
    class Node {
        constructor(element = null, next = null) {
            this.element = element;
            this.next = next;
        }
    }
    // let n2 = new Node("world");
    // let n1 = new Node("hello", n2);
    // console.log(n1.element, n1.next);
    class LinkList {
        constructor() {
            this.head = new Node("head");
        }
        find(item) {
            let currNode = this.head;
            while (currNode.next) {
                if (currNode.element === item) {
                    break;
                }
                else {
                    currNode = currNode.next;
                }
            }
            return currNode;
        }
        findPrevious(item) {
            // 查找一个元素的前一个元素
            let currNode = this.head;
            while (currNode.next) {
                if (currNode.next.element === item) {
                    break;
                }
                else {
                    currNode = currNode.next;
                }
            }
            return currNode;
        }
        insert(newElement, item) {
            let node = new Node(newElement);
            let currNode = this.find(item);
            node.next = currNode.next;
            currNode.next = node;
        }
        remove(item) {
            let prevNode = this.findPrevious(item);
            if (prevNode.next) {
                prevNode.next = prevNode.next.next;
            }
        }
        display() {
            let currNode = this.head;
            while (currNode) {
                console.log(currNode.element);
                if (currNode.next) {
                    currNode = currNode.next;
                }
                else {
                    break;
                }
            }
        }
    }
    let list = new LinkList();
    list.insert("Jimmy", "head");
    list.insert("xuexue", "jimmy");
    list.insert("Henry", "xuexue");
    list.insert("Jack", "Henry");
    list.display();
    list.remove("Jack");
    console.log("---------");
    list.display();
}
